package com.common.exception;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.common.http.HttpResult;
import com.common.http.HttpStatus;

@ControllerAdvice
public class MyGlobalExceptionHandler {

	static final Logger logger = LoggerFactory.getLogger(MyGlobalExceptionHandler.class);

	@ExceptionHandler(Exception.class)
	@ResponseBody
	public HttpResult customException(Exception e) {
//        ModelAndView mv = new ModelAndView();
//        mv.addObject("message", e.getMessage());
//        mv.setViewName("myerror");
		logger.error("globalexception:{}", e);
		JSONObject jsonObject = new JSONObject();
		JSONArray jsonArray = new JSONArray();
		HttpResult httpResult = new HttpResult();
		httpResult.setCode(HttpStatus.CODE_60601);
		httpResult.setData(jsonObject);
		httpResult.setRows(jsonArray);
		httpResult.setMsg(e.getMessage());
		return httpResult;
//        return mv;
	}

	@ExceptionHandler(java.io.FileNotFoundException.class)
	@ResponseBody
	public HttpResult customException1(java.io.FileNotFoundException e) {
//        ModelAndView mv = new ModelAndView();
//        mv.addObject("message", e.getMessage());
//        mv.setViewName("myerror"); 
		logger.error("globalexception:{}", e);
		JSONObject jsonObject = new JSONObject();
		JSONArray jsonArray = new JSONArray();
		HttpResult httpResult = new HttpResult();
		httpResult.setCode(HttpStatus.CODE_60601);
		httpResult.setData(jsonObject);
		httpResult.setRows(jsonArray);
		httpResult.setMsg("文件名称不能包含特殊字符！");
		return httpResult;
//        return mv;
	}

	@ExceptionHandler(com.alibaba.excel.exception.ExcelGenerateException.class)
	@ResponseBody
	public HttpResult customException1(com.alibaba.excel.exception.ExcelGenerateException e) {
//        ModelAndView mv = new ModelAndView();
//        mv.addObject("message", e.getMessage());
//        mv.setViewName("myerror"); 
		logger.error("globalexception:{}", e);
		JSONObject jsonObject = new JSONObject();
		JSONArray jsonArray = new JSONArray();
		HttpResult httpResult = new HttpResult();
		httpResult.setCode(HttpStatus.CODE_60601);
		httpResult.setData(jsonObject);
		httpResult.setRows(jsonArray);
		httpResult.setMsg("文件名称不能包含特殊字符！");
		return httpResult;
//        return mv;
	}

	@ExceptionHandler(com.alibaba.excel.exception.ExcelDataConvertException.class)
	@ResponseBody
	public HttpResult customException1(com.alibaba.excel.exception.ExcelDataConvertException e) {
//        ModelAndView mv = new ModelAndView();
//        mv.addObject("message", e.getMessage());
//        mv.setViewName("myerror"); 
		logger.error("globalexception:{}", e);
		JSONObject jsonObject = new JSONObject();
		JSONArray jsonArray = new JSONArray();
		HttpResult httpResult = new HttpResult();
		httpResult.setCode(HttpStatus.CODE_60601);
		httpResult.setData(jsonObject);
		httpResult.setRows(jsonArray);
		httpResult.setMsg("格式转换错误，请检查是否数字录入错误！");
		return httpResult;
//        return mv;
	}

}
